//
//  StatisticsViewController.m
//  KeepAccounts
//
//  Created by honeyrr on 2017/10/1.
//  Copyright © 2017年 honeyrr. All rights reserved.
//

#import "KAStatisticsViewController.h"
#import "KADayViewController.h"
#import "KAMonthViewController.h"
#import "KAYearViewController.h"


@interface KAStatisticsViewController ()<UIScrollViewDelegate>

@property(nonatomic, strong) UISegmentedControl *segmentControl;
//水平滚动容器
@property (nonatomic, weak) UIScrollView *contentView;

@end

@implementation KAStatisticsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self setUpUI];
}

- (UIScrollView *)contentView
{
    if(_contentView == nil)
    {
        UIScrollView *view = [[UIScrollView alloc] init];
        view.showsHorizontalScrollIndicator = NO;
        view.delegate = self;
        view.backgroundColor = [UIColor whiteColor];
        view.bounces = NO;
        view.alwaysBounceHorizontal = YES;
        view.alwaysBounceHorizontal = NO;
        _contentView = view;
        view.pagingEnabled = YES;
        [self.view addSubview:view];
    }
    return _contentView;
}

-(void) setUpUI
{
    UIView *view = [UIView new];
    view.frame = CGRectMake(0, 0, KAScreenW, 44);
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 60, 44)];
    label.text = @"明细";
    label.textColor = [UIColor whiteColor];
    label.font = [UIFont systemFontOfSize:18];
    [view addSubview:label];
    [view addSubview:self.segmentControl];
    
    self.navigationItem.titleView = view;
    
    self.segmentControl.selectedSegmentIndex = 0;
    
    self.contentView.frame = CGRectMake(0, 64, KAScreenW, KAScreenH - 48);
    
    NSArray *vcArr = @[[KADayViewController new], [KAMonthViewController new], [KAYearViewController new]];
    for (int i = 0; i < vcArr.count; i++) {
        [[vcArr[i] view] setFrame:CGRectMake(i * KAScreenW, 0, self.contentView.frame.size.width, self.contentView.frame.size.height)];
        [self addChildViewController:vcArr[i]];
        [self.contentView addSubview:[vcArr[i] view]];
    }
    self.contentView.contentSize = CGSizeMake(KAScreenW * vcArr.count, KAScreenH - 49 - 64);
}

-(UISegmentedControl *)segmentControl
{
    if (!_segmentControl) {
        UISegmentedControl *sc = [[UISegmentedControl alloc]initWithItems:@[@"日", @"月", @"年"]];
        sc.bounds = CGRectMake(0, 0, 135, 30);
        sc.center = CGPointMake(KAScreenW  / 2 , 44 / 2);
        sc.apportionsSegmentWidthsByContent = YES;
        [sc addTarget:self action:@selector(segmentedControlClicked:) forControlEvents:UIControlEventValueChanged];
        _segmentControl = sc;
    }
    return _segmentControl;
}

-(void) segmentedControlClicked:(UISegmentedControl *)sender
{
    NSInteger index = sender.selectedSegmentIndex;
    [self.contentView setContentOffset:CGPointMake(KAScreenW * index, 0) animated:YES];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    //设置标题栏的位置
    NSInteger index = scrollView.contentOffset.x / KAScreenW;
    self.segmentControl.selectedSegmentIndex = index;
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    [self scrollViewDidEndDecelerating:scrollView];
}
@end
